<?php
namespace App\Http\Controllers\Admin;

use Illuminate\Support\Facades\Response;
use Illuminate\Http\Request;

use App\Models\UserReport;
use App\Models\UserInbox;



class UserReportController extends BaseController
{
    protected $_app = [
        'companyquestions' => '社区问答',
        'employ'           => '就业指南',
        'teaches'           => '充电加油',
        'jobs'             => '招聘职位'
    ];
    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index(Request $request)
    {
         if(!$this->user->can('viewUserReport')) abort(405);
        // 搜索
        $post = $request->input();
        $report = new UserReport;
        if(is_array($post) AND !empty($post))
        {
            $reports = $report->where('reson' , 'LIKE', '%' . $post['keywords'] . '%')
                      ->where('app', '=', $post['app'])
                      ->where(function($query) use ($post) {
                            $query->where('created_at', '>=', $post['at_start'])
                                  ->where('created_at', '<=', $post['at_end']);
                        })
                      ->paginate(20);
        } else
        {
            $reports = $report->paginate(20);
        }
        foreach ($reports as $key => $value) {
            $app = $value->$value['app'];
            if(count($app) > 0)
                $reports[$key]['app_name'] = $app->title;
            else
                $reports[$key]['app_name'] = '已被删除';
            $reports[$key]['app'] = $this->_app[$value['app']];
        }
        // var_dump($reports);
        return view('admin.users.report.index')->with('reports', $reports)
                ->with('apps', $this->_app);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return Response
     */
    public function create()
    {
        if(!$this->user->can('editUserReport')) abort(405);

        return  view('admin.organization.add')->with('type',$this->catgory);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  Request  $request
     * @return Response
     */
    public function store(Request $request)
    {
//
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function show($id)
    {
        //
    }

    /**
     * 编辑机构信息
     *
     * @param  int  $id
     * @return Response
     */
    public function edit($id)
    {
    //        if(!$this->user->can('editEmployCategory')) abort(403);

    }

    /**
     * 更新机构信息
     *
     * @param  Request  $request
     * @param  int  $id
     * @return Response
     */
    public function update(Request $request, $id)
    {
        //

    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function destroy(Request $request)
    {
        if(!$this->user->can('deleteUserReport')) 
            return $this->formatJson('error',$this->permerror);
        
        $id = $request->input('id');
        $inbox = UserInbox::find($id);
        $inbox->delete();
        return  Response::json(array('status'=>'success','msg'=>'私信：［'.str_limit($inbox->message, 15).'］ 删除成功'));
    }

    /**
     * 批量删除
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function deletes(Request $request)
    {
        if(!$this->user->can('deleteUserReport'))
            return $this->formatJson('error',$this->permerror);
        
        $ids = explode(',', $request->input('id'));
        $inbox = UserInbox::whereIn('id', $ids);
        $count = $inbox->count();
        if ($inbox->delete())
            return  Response::json(array('status'=>'success','msg'=>'成功删除 [' . $count . ']条 私信'));
        else
            return  Response::json(array('status'=>'error','msg'=>'删除失败'));
    }

}
